<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>开门</title>
    <style>
      body {
        box-sizing: border-box;
        padding: 160px 0 0 240px;
      }

      /* 门容器样式 */
      .doorWrap {
        width: 320px;
        height: 320px;
        border: 1px solid #666;
        perspective: 500px;
        position: relative;
        display: flex;
      }

      /* 左门右门的共有样式 */
      .leftDoor,
      .rightDoor {
        width: 50%;
        height: 100%;
        background-color: skyblue;
        transition: 1.2s;
        z-index: 2;
        display: flex;
        justify-content: center;
        align-items: center;
        border: 0.1px solid #fff;
      }

      /* 设置旋转元素的基点，左边大门以左侧为基准点旋转 */
      .leftDoor {
        transform-origin: left;
      }

      .rightDoor::after,
      .leftDoor::after {
        content: attr(data-text);
        writing-mode: vertical-lr;
        transform: rotateY(130deg);
      }

      /* 设置旋转元素的基点，右边大门以右侧为基准点旋转 */
      .rightDoor {
        transform-origin: right;
      }

      /* 当鼠标悬浮的时候，设置开门的幅度，左门往左侧开 */
      .doorWrap:hover .leftDoor {
        transform: rotateY(-130deg);
      }

      /* 右门往右侧开 */
      .doorWrap:hover .rightDoor {
        transform: rotateY(130deg);
      }

      /* 内容区的定位层级稍微低一些 */
      .content {
        position: absolute;
        z-index: 1;
        width: 100%;
        height: 100%;
        background-color: green;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    </style>
  </head>

  <body>
    <div class="doorWrap" id="doorWrap">
      <div class="leftDoor" id="leftDoor">left</div>
      <div class="rightDoor" id="rightDoor">right</div>
      <div class="content">Hello world...</div>
    </div>

    <script>
      const doorWrap = document.getElementById('doorWrap');
      const leftDoor = document.getElementById('leftDoor');
      const rightDoor = document.getElementById('rightDoor');

      const animationDuration = 400; // 动画持续时间（毫秒）

      doorWrap.addEventListener('mouseenter', () => {
        setTimeout(() => {
          leftDoor.innerHTML = ''; // 显示左门文字
          rightDoor.innerHTML = ''; // 显示右门文字

          // 设置 data-text 属性
          leftDoor.setAttribute('data-text', '风调雨顺');
          rightDoor.setAttribute('data-text', '国泰明安');
        }, animationDuration); // 等待动画结束再显示文字
      });

      doorWrap.addEventListener('mouseleave', () => {
        setTimeout(() => {
          leftDoor.innerHTML = 'left'; // 显示左门文字
          rightDoor.innerHTML = 'right'; // 显示右门文字

          // 设置 data-text 属性
          leftDoor.setAttribute('data-text', '');
          rightDoor.setAttribute('data-text', '');
        }, animationDuration); // 等待动画结束再显示文字
      });
    </script>
  </body>
</html>
